/**
 * Project Name:study_java_j2se
 * File Name:TestConnectionDB.java
 * Package Name:com.cb.ads.sjj.user.eight
 * Date:2015年12月30日下午5:30:57
 * Copyright (c) 2015, erwin@163.com All Rights Reserved.
 *
 */

package com.clps.dev.sms.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;

/**
 * ClassName: TestConnectionDB <br/>
 * Function: 
 * Reason: 
 * date: 2015年12月30日 下午5:30:57 <br/>
 * 
 * @author erwin.wang
 * @version 1.0
 * @since JDK 1.8
 * @TestConnectionDB
 */
public class ConnectDB {

	private static Logger log = Logger.getLogger(ConnectDB.class);
	
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/scb_sms";
	private static String username = "root";
	private static String password = "123";

	private static Statement stmt = null;

	private static Connection conn = null;

	private static PreparedStatement pstmt = null;

	private static ResultSet rs = null;

	/**
	 * 
	 * getConnectDB: get a object of db connect <br/> 
	 * @return Connection
	 */
	public static Connection getConnectDB() {
		/*this.driver = ReadProperties.getDriver();
		this.url = ReadProperties.getUrl();
		this.username = ReadProperties.getUsername();
		this.password = ReadProperties.getPassword();*/
		try {
			Class.forName(driver).newInstance();						//驱动实例化
			conn = DriverManager.getConnection(url, username, password);
		} catch (InstantiationException e) {
			log.error("Driver Intantiation Exception");
		} catch (IllegalAccessException e) {
			log.error("Illegal Access Exception");
		} catch (ClassNotFoundException e) {
			log.error("Class Not Found Exception ");
		} catch (SQLException e) {
			log.error("SQL Exception");
		}
		return conn;
	}

	/**
	 * 
	 * freeAll:stop all db connect object <br/> 
	 *
	 */
	public void freeAll() {
		try {
			if (rs != null) {
				ConnectDB.rs.close();
			}
		} catch (SQLException e) {
			log.error("Close Result Set Exception");
		}
		try {
			if (stmt != null) {
				ConnectDB.stmt.close();
			}
		} catch (SQLException e) {
			log.error("Close SQL Exception For Statement");
		}
		try {
			if (pstmt != null) {
				ConnectDB.pstmt.close();
			}
		} catch (SQLException e) {
			log.error("Close SQL Exception For PreparedStatement");
		}
		try {
			if (conn != null) {
				ConnectDB.conn.close();
			}
		} catch (SQLException e) {
			log.error("Close SQL Exception For Connection");
		}
	}

}
